/****** Object:  Stored Procedure [dbo].spBienBanKiemKeKhoInsert Script Date: 7/16/2012 ******/
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'[dbo].[spBienBanKiemKeKhoInsert]') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
	DROP PROCEDURE [dbo].[spBienBanKiemKeKhoInsert]
GO

/******************************************************************************
**		Change History
*******************************************************************************
**		Date:		Author:					Description:
**		--------	--------				-----------------------------------
**		17/07/2012	SongTM					Innit created
*******************************************************************************/

CREATE PROCEDURE [dbo].[spBienBanKiemKeKhoInsert]
(
	@MaBBKKK nvarchar(12) output,
	@Thang tinyint,
	@Nam smallint,
	@NgayThucHien datetime,
	@NguoiThucHien nvarchar(35),
	@ThuKho nvarchar(35),
	@KeToan nvarchar(35),
	@GiamDoc nvarchar(35),
	@KetLuan tinyint,
	@NguyenNhan nvarchar(300),
	@YKien nvarchar(300),
	@MaKy nvarchar(8)
)
WITH ENCRYPTION

AS

SET NOCOUNT ON
Declare @Stt char(3)
if not exists (select * from [tbl_BienBanKiemKeKho])
Set @Stt=1
else
Set @Stt=(select MAX(RIGHT(LEFT([MaBBKKK],6),3)) from [tbl_BienBanKiemKeKho])+1
Set @Stt=REPLICATE('0',3-LEN(@Stt))+@Stt
set @Thang = REPLICATE('0',2-LEN(@Thang))+@Thang
Set @MaBBKKK='KKKooo/mmyy'
Set @MaBBKKK=REPLACE(@MaBBKKK,'ooo',@Stt);
Set @MaBBKKK=REPLACE(@MaBBKKK,'mm',@Thang);
Set @MaBBKKK=REPLACE(@MaBBKKK,'yy',@Nam);

INSERT INTO [tbl_BienBanKiemKeKho]
(
	[MaBBKKK],
	[Thang],
	[Nam],
	[NgayThucHien],
	[NguoiThucHien],
	[ThuKho],
	[KeToan],
	[GiamDoc],
	[KetLuan],
	[NguyenNhan],
	[YKien],
	[MaKy]
)
VALUES
(
	@MaBBKKK,
	@Thang,
	@Nam,
	@NgayThucHien,
	@NguoiThucHien,
	@ThuKho,
	@KeToan,
	@GiamDoc,
	@KetLuan,
	@NguyenNhan,
	@YKien,
	@MaKy
)
GO

--GRANT EXECUTE ON [dbo].[spBienBanKiemKeKhoInsert] TO [sa]
--GO
